package com.zhongfl.crm.lixiao.common;

import com.zhongfl.crm.lixiao.config.MailConfigInfo;
import com.zhongfl.crm.mail.MailService;
import com.zhongfl.crm.mail.bean.MailMessage;
import com.zhongfl.crm.mail.bean.MailMessageBuilder;
import com.zhongfl.crm.mail.enums.MailTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 励销同步工具类
 * @author zhangzengke
 * @date 2018/9/26 14:53
 */
@Service
@Slf4j
public class LxSyncUtils {

    @Resource
    private MailConfigInfo mailConfigInfo;

    @Resource
    private MailService mailService;

    /**
     * 发送错误邮件
     *
     * @param syncErrorList 同步错误信息容器
     */
    public void sendNotifyMail(List<Map<String, Object>> syncErrorList, MailTypeEnum mailTypeEnum, String title) {
        try {
            if (syncErrorList.isEmpty()) {
                return;
            }

            if (CollectionUtils.isEmpty(syncErrorList)) {
                return;
            }
            MailMessage mailMessage = MailMessageBuilder.builder()
                    .mailTypeEnum(mailTypeEnum)
                    .subject(title + mailConfigInfo.getActiveProfiles())
                    .to(mailConfigInfo.getDefaultTo())
                    .addData("title", title)
                    .addData("syncErrorList", syncErrorList)
                    .build();

            mailService.send(mailMessage);
        } catch (Exception ex) {
            log.error("发送邮件异常: {}", ex.getMessage(), ex);
        }
    }

    public void sendNotifyMail(String syncError, MailTypeEnum mailTypeEnum, String title){
        try {
            MailMessage mailMessage = MailMessageBuilder.builder()
                    .mailTypeEnum(mailTypeEnum)
                    .subject(title + mailConfigInfo.getActiveProfiles())
                    .to(mailConfigInfo.getDefaultTo())
                    .addData("title", title)
                    .addData("message", syncError)
                    .build();

            mailService.send(mailMessage);
        } catch (Exception ex) {
            log.error("发送邮件异常: {}", ex.getMessage(), ex);
        }
    }

}
